import { common_getAreaMap } from "../util/area"
const deliverModule = {
    namespaced: true,
    state: {
        goodsStockList: []
    },
    mutations: {
        addStockInfo(state, palyload) {
            state.goodsStockList.push(palyload)
        },
        resetStock(state, palyload) {
            state.goodsStockList = []
        }
    },
    actions: {
        //刷新库存
        refreshStock({ state, commit, rootState, dispatch }) {
            commit("resetStock")
            dispatch("checkStock")
        },
        //查询库存
        checkStock({ state, commit, rootState }) {
            var province = common_getAreaMap().provinceMap
            let index = 0
            let keys = Object.keys(province)
            function areaCheck() {
                if (keys[index]) {
                    setTimeout(() => {
                        chrome.runtime.sendMessage({
                            type: "custom_header",
                            url: "/getWareBusiness",
                            baseURL: "https://item-soa.jd.com",
                            params: { skuId: rootState.id, area: keys[index] } 
                          }, function (data) {
                            if (data) {
                                commit("addStockInfo", {
                                    province: province[keys[index]],
                                    price: data.price.p,
                                    stockState: data.stockInfo.stockState,
                                    stockDesc: data.stockInfo.stockDesc,
                                    stock: data.stockInfo.stockState,
                                })
                            }
                            index++
                            areaCheck()
                          })
                    }, 300)
                }
            }
            areaCheck()

        }
    }
}

export default deliverModule